﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 项目框架
{
    public class AsyncForm
    {
        private Form frm;
        public AsyncForm(Form frmPram) 
        {
            this.frm = frmPram;
        }
        public void Show()
        {
            //MethodInvoker methodInvoker = new MethodInvoker(ShowDialog);
            //methodInvoker.BeginInvoke(null,null);
            MethodInvoker methodInvoker = new MethodInvoker(ShowDialog);
            methodInvoker.BeginInvoke(null, null);
            while (!frm .IsHandleCreated) { }
            // 把显示窗体放到最前面
            frm.Invoke(new MethodInvoker(() =>
            {
                frm.BringToFront();//放在前端显示
                frm.Activate(); //当前窗体是LoadingForm
            }));

        }
        public void Close()
        {
            if (frm.IsHandleCreated)
            {
                frm.BeginInvoke(new Action(() =>
                {
                    this.frm.Dispose();
                }));
            }
        }
        private void ShowDialog()
        {
            //用Show()方法会开死
            frm.ShowDialog();
        }
    }
}
